package com.faf.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.faf.bean.User;
import com.faf.dao.UserDao;

public class UserDaoImpl extends BaseDaoImpl implements UserDao
{
	public UserDaoImpl(){
		
	}
	
	public UserDaoImpl(Connection con)
	{
		super(con);
	}
	
    public void insert(User user) throws SQLException
    {
    	String sql = "insert into user (userName,password,money,tel) values (?,?,?,?)";
    
        executeUpdate(sql);
    
        //close();
    }
  //账号登陆
    public User getUserByNumber(String userNumber) throws SQLException
    {
    	String sql = "select userId,userNumber,userPassword,userName,userSex,userAge,userhometown,userAddress, userPhone,userSignDate from user where userNumber = ?";
    	
    	executeQuery(sql,userNumber);

		User u = new User();
    	
    	while(rs.next()){
    		u.setUserId(rs.getInt(1));
    		u.setUserNumber(rs.getString(2));
        	u.setUserPassword(rs.getString(3));
        	u.setUserName(rs.getString(4));
        	u.setUserSex(rs.getString(5));
        	u.setUserAge(rs.getInt(6));
        	u.setUserHometown(rs.getString(7));
        	u.setUserAddress(rs.getString(8));
        	u.setUserPhone(rs.getString(9));
        	u.setUserSignDate(rs.getString(10));
    	}
		
    	close();
    	super.con = null;
    	return u;
    }   
    //手机号登陆
    public User getUserByPhone(String phone) throws SQLException
    {
    	String sql = "select userId,userNumber,userPassword,userName,userSex,userAge,userhometown,userAddress, userPhone,userSignDate from user where userPhone = ?";
    	
    	executeQuery(sql,phone);

		User u = new User();
    	
    	while(rs.next()){
    		u.setUserId(rs.getInt(1));
    		u.setUserNumber(rs.getString(2));
        	u.setUserPassword(rs.getString(3));
        	u.setUserName(rs.getString(4));
        	u.setUserSex(rs.getString(5));
        	u.setUserAge(rs.getInt(6));
        	u.setUserHometown(rs.getString(7));
        	u.setUserAddress(rs.getString(8));
        	u.setUserPhone(rs.getString(9));
        	u.setUserSignDate(rs.getString(10));
    	}
		System.out.println("用户密码为"+u.getUserPassword());
    	close();
    	super.con = null;
    	return u;
    }

	@Override
	public List<User> getUserList() throws SQLException
	{
		String sql = "select userId, userNumber,userpassword,userName,userSex,userAge,userHometown,userAddress,"
				+ "userPhone,userSignDate,messageRemark from user";
		executeQuery(sql);
		List<User> userList = new ArrayList<User>();
		while(rs.next()) {
			User user = new User(rs.getInt("userId"),rs.getString("userNumber"),rs.getString("userPassword"),rs.getString("userName"),
					rs.getString("userSex"),rs.getInt("userAge"),rs.getString("userHometown"),rs.getString("userAddress"),
					rs.getString("userPhone"),rs.getString("userSignDate"),rs.getString("messageRemark"));
			userList.add(user);
		}
		close();
    	super.con = null;
    	return userList;
	}
    
}

